<?php

namespace app\admin\controller\user;

use addons\shopro\service\Wallet as WalletService;
use app\admin\model\shopro\commission\Agent;
use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class User extends Backend
{

    protected $relationSearch = true;
    protected $searchFields = 'id,username,nickname';

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\User;
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with('group')
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
                $v->hidden(['password', 'salt']);

            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
        }
        return parent::add();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $this->token();
        }
        $row = $this->model->get($ids);
        $this->modelValidate = false;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
        return parent::edit($ids);
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        $row = $this->model->get($ids);
        $this->modelValidate = true;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        Auth::instance()->delete($row['id']);
        $this->success();
    }


    public function funds($ids){
        if(request()->post()){
            $data = $this->request->only(['currency_id','account']);
            if(empty($data['currency_id'])) return json(['code'=>0,'msg'=>'请选择操作类型']);
            if(empty($data['account'])) return json(['code'=>0,'msg'=>'请输入数量']);
            $account = $data['account'];
            $currency='';
            switch ($data['currency_id']){
                case 1:
                    $currency='money';
                    break;
                case 2:
                    $currency='commission';
                    break;
                case 3:
                    $currency='tokens';
                    break;
                case 4:
                    $currency='score';
                    break;
                case 5:
                    $currency='gold_bean';
                    break;
                case 6:
                    $currency='shopping_score';
                    break;
                case 7:
                    $currency='total_score';
                    break;
            }
             
            if(in_array($currency,['money','commission','tokens','score','total_score', 'shopping_score', 'gold_bean'])){
                Db::startTrans();
                if(WalletService::change($ids,$currency,$account,'admin_recharge')){
                    
                    Db::commit();
                }else{
                    Db::rollback();
                    return json(['code'=>0,'msg'=>'充值失败']);
                }

            }
            return json(['code'=>1,'msg'=>'充值成功']);


        }
        $userInfo = $this->model->field('mobile,money,commission,score,total_score,tokens,username,nickname,gold_bean,shopping_score')->find($ids);
        $currencyData=[
            ['id'=>1,'name'=>'消费券'],
            ['id'=>2,'name'=>'福利券'],
            ['id'=>3,'name'=>'通证'],
            ['id'=>4,'name'=>'贡献值'],
            ['id'=>5,'name'=>'金豆'],
            ['id'=>6,'name'=>'购物积分'],
            ['id'=>7,'name'=>'总贡献值'],
        ];
        $this->assign('userInfo',$userInfo);
        $this->assign('currencyData',$currencyData);
        $this->assign('ids',$ids);
        return $this->fetch();
    }


    public function newscore($ids){

        if(request()->isAjax()){
            Db::startTrans();
            $user=\app\admin\model\shopro\user\User::get($ids);
            if($user){
                $user->total_score=$user->score;
                $user->save();
                //Agent::where('user_id',$user->id)->setInc('score_total_all',$userLucky['score']);
                Db::commit();
                return json(['code'=>1,'msg'=>'重置成功']);
            }
            Db::rollback();
            return json(['code'=>0,'msg'=>'重置失败']);
        }


    }


}
